﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Common.Enums;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Common.MathUtils;
using ZhimeiSoft.Framework.Client.Base;


namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmAddCustomerPayment : FormBase
    {
        public ControlClassModeEnum Flag { get; set; }
        private DBEntityCustomerPayment _entity;
        private int _customerId = -1;
        private int _serviceItemId = -1;

        private SortedList<string, DBEntityEmployee> listEmployee;

        private SortedList<string, DBEntityServiceItem> listServiceItem;

        public frmAddCustomerPayment()
        {
            InitializeComponent();
            this.customSearch1.onSerchCustom = this.serchCustomInfo;
        }

        public frmAddCustomerPayment(DBEntityCustomerPayment entity)
        {
            InitializeComponent();
            this._entity = entity;
        }

        
        private void serchCustomInfo(DataRow dr)
        {
            if (dr == null)
                return;

            string sName = dr[DBEntityCustomInfo.FieldName.CNAME].ToString();
            string sPhone = dr[DBEntityCustomInfo.FieldName.CMOBILEPHONE].ToString();
            string sNickName = dr[DBEntityCustomInfo.FieldName.CNICKNAME].ToString();
            string sLastVisited = dr[DBEntityCustomInfo.FieldName.CLASTREGISTERDATE].ToString();

            lblName.Text = sName;
            lblPhone.Text = sPhone;
            lblNickName.Text = sNickName;
            lblLastVisted.Text = sLastVisited;

            _customerId = Convert.ToInt32(dr[DBEntityCustomInfo.FieldName.CUSTOMERID]);
            LoadCustomerServiceItem(_customerId);
        }

        private void cbeServiceItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sServiceItemID = cbeServiceItem.SelectedValue.ToString();
            if (listServiceItem.Keys.Contains(sServiceItemID))
            {
                DBEntityServiceItem entity = listServiceItem[sServiceItemID];
                txtServiceItemCode.Text = entity.SERVICEITEMCODE;
                txtPrice.Text = entity.SPRICE.ToString();
                txtTimespan.Text = entity.STIMESPAN.ToString();
                _serviceItemId = entity.SERVICEITEMID;
            }
        }

        private void LoadCustomerServiceItem(int iCustomerID)
        {
            string error = string.Empty;
            DataSet ds = new DataSet();
            string sFilter = string.Format("{0}={1}", DBEntityCustomerServiceItem.FieldName.CUSTOMERID, iCustomerID.ToString());
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerServiceItem(sFilter, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员服务项目信息失败\r\n" + error);
                return;
            }
            listServiceItem = new SortedList<string, DBEntityServiceItem>();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string serviceItemID = dr[DBEntityServiceItem.FieldName.SERVICEITEMID].ToString();
                DBEntityServiceItem entity = new DBEntityServiceItem(dr);
                if (!listServiceItem.Keys.Contains(serviceItemID))
                {
                    listServiceItem.Add(serviceItemID, entity);
                }
            }

            cbeServiceItem.DataSource = ds.Tables[0].DefaultView;
            cbeServiceItem.ValueMember = DBEntityServiceItem.FieldName.SERVICEITEMID;
            cbeServiceItem.DisplayMember = DBEntityServiceItem.FieldName.SNAME;
        }

        private void LoadEmployeeData()
        {
            string error = string.Empty;
            DataSet ds = new DataSet();
            string state = DBEntityEmployee.ENUM_ESTATE.F0E;
            bool bFlag = BussinessProxyManager.Instance.QueryEmployeeState(state, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取员工信息失败\r\n" + error);
                return;
            }

            listEmployee = new SortedList<string, DBEntityEmployee>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DBEntityEmployee entity = new DBEntityEmployee(dr);
                string employeeId = dr[DBEntityEmployee.FieldName.EMPLOYEEID].ToString();
                if (!listEmployee.Keys.Contains(employeeId))
                    listEmployee.Add(employeeId, entity);
            }

            cbeEmployee.DataSource = ds.Tables[0].DefaultView;
            cbeEmployee.ValueMember = DBEntityEmployee.FieldName.EMPLOYEEID;
            cbeEmployee.DisplayMember = DBEntityEmployee.FieldName.ENAME;
        }

        private void LoadPayTypeData()
        {
            string error = string.Empty;
            DataSet ds = new DataSet();
            string filter = string.Empty;
            bool bFlag = BussinessProxyManager.Instance.QueryPayType(filter, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取充值方式失败\r\n" + error);
                return;
            }

            cbePayType.DataSource = ds.Tables[0].DefaultView;
            cbePayType.ValueMember = DBEntityPayType.FieldName.PAYTYPEID;
            cbePayType.DisplayMember = DBEntityPayType.FieldName.PNAME;
        }

        private void LoadCustomerData(int iCustomerID)
        {
            string error = string.Empty;
            DataSet ds = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerID(iCustomerID, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员信息失败\r\n" + error);
                return;
            }
            if (ds.Tables[0].Rows.Count == 1)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                serchCustomInfo(dr);
            }
        }

        private bool ValidateFormInput()
        {
            string error = "";
            if (_customerId == -1)
            {
                error = "请选择会员。";
                MsgBox.MsgInfo(error);
                return false;
            }

            if (_serviceItemId == -1)
            {
                error = "请选择服务项目。";
                MsgBox.MsgInfo(error);
                cbeServiceItem.Focus();
                SetErrorControl(cbeServiceItem, error);
                return false;
            }
            
            if (dtiPay.Text.Trim() == string.Empty)
            {
                error = "请输入充值日期。";
                MsgBox.MsgInfo(error);
                dtiPay.Focus();
                SetErrorControl(dtiPay, error);
                return false;
            }

            string money = txtMoney.Text.Trim();
            if (money == string.Empty)
            {
                error = "请输入充值金额。";
                MsgBox.MsgInfo(error);
                txtMoney.Focus();
                SetErrorControl(txtMoney, error);
                return false;
            }
            if (!NumberUtil.IsNonNegativeDecimal(money))
            {
                error = "充值金额必须为数字。";
                MsgBox.MsgInfo(error);
                txtMoney.Focus();
                SetErrorControl(txtMoney, error);
                return false;
            }

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateFormInput())
                return;

            bool bFlag = false;
            string error = string.Empty;

            DBEntityCustomerServiceItem itementity = ServiceItemCCtrl.GetCustomerServiceItem(_customerId, _serviceItemId);
            if (itementity == null)
                return;

            int iCustomerServiceItemID = itementity.CUSTOMERSERVICEITEMID;
            if (this.Flag == ControlClassModeEnum.emInsert)
            {
                _entity = new DBEntityCustomerPayment();
                _entity.CEmployeeID = Convert.ToInt32(cbeEmployee.SelectedValue);
                _entity.CPayDate = dtiPay.Value;
                _entity.CMemo = txtMemo.Text;
                _entity.PayTypeID = Convert.ToInt32(cbePayType.SelectedValue);
                _entity.CustomerServiceItemID = iCustomerServiceItemID;
                _entity.CMoney = Convert.ToDecimal(txtMoney.Text.Trim());
                bFlag = BussinessProxyManager.Instance.InsertCustomerPayment(_entity, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("新增会员充值信息失败\r\n" + error);
                    return;
                }
                
            }
            else
            {
                _entity.CEmployeeID = Convert.ToInt32(cbeEmployee.SelectedValue);
                _entity.CPayDate = dtiPay.Value;
                _entity.CMemo = txtMemo.Text;
                _entity.PayTypeID = Convert.ToInt32(cbePayType.SelectedValue);
                _entity.CustomerServiceItemID = iCustomerServiceItemID;
                _entity.CMoney = Convert.ToDecimal(txtMoney.Text.Trim());
                bFlag = BussinessProxyManager.Instance.UpdateCustomerPayment(_entity, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("更新会员充值信息失败\r\n" + error);
                    return;
                }
            }
            MsgBox.MsgInfo("保存成功。");
            this.DialogResult = DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmAddCustomerPayment_Shown(object sender, EventArgs e)
        {
            LoadEmployeeData();
            LoadPayTypeData();
            txtMoney.Enabled = true;
            if (Flag == ControlClassModeEnum.emEdit)
            {
                int iCustomerServiceItemID = _entity.CustomerServiceItemID;
                _customerId = _entity.CustomerID;
                _serviceItemId = _entity.ServiceItemID;

                LoadCustomerData(_customerId);
                //LoadCustomerServiceItem(_customerId);

                cbeServiceItem.SelectedValue = _serviceItemId;
                cbeServiceItem_SelectedIndexChanged(null, null);

                cbePayType.SelectedValue = _entity.PayTypeID;
                cbeEmployee.SelectedValue = _entity.CEmployeeID;
                txtMemo.Text = _entity.CMemo;
                dtiPay.Value = _entity.CPayDate;
                txtMoney.Text = _entity.CMoney.ToString();
                txtMoney.Enabled = false;
            }
        }
    }
}
